import { _decorator } from 'cc';
import { LayerType } from '../../Basic/Config/BaseEnum';
import { MultiPlatform } from '../../Basic/Model/Platform/MultiPlatform';
import { VideoEvent } from '../../Data/Types';
import { EventManager } from '../../Basic/Manager/EventManager';
import { EventType } from '../Config/GameEnum';
import { DataMgr, PlayerData } from '../../Basic/Manager/DataManager';
import { GEEvent, GravityModel } from '../../Basic/Model/SDK/GravityModel';
import { PlayerParams } from '../../Basic/Config/DataConfig';
import { PanelBase } from '../../Basic/UI/PanelBase';
import { GameModel } from '../Game/GameModel';
import { UnlockItemModel } from './UnlockItemModel';
const { ccclass } = _decorator;

@ccclass('UnlockItemPanel')
export class UnlockItemPanel extends PanelBase {
  panelLayer: LayerType.DialogLayer = LayerType.DialogLayer;

  adTimer = this.onAd.bind(this);
  protected onEnable(): void {
    super.onEnable();
    EventManager.Ins().register(EventType.AD_GRID, this.onAd, this);
  }

  protected onDisable(): void {
    EventManager.Ins().unRegister(EventType.AD_GRID, this.onAd);
  }

  onAd() {
    GameModel.Ins().adCount++;
    DataMgr.changePlayerAttr(PlayerParams.adCount, 1);
    // GravityModel.I.userSet({ accuAds: PlayerData.get(PlayerParams.adCount) });
    // GravityModel.I.track(GEEvent.AD_SHOW_END, { adPosition: `看视频解锁格子` });
    GameModel.Ins().cellMachine.unlockItem(UnlockItemModel.Ins().unlockCol, UnlockItemModel.Ins().unlockRow);
    this.clickClose();
  }

  onClickBtn(t: Touch, data) {
    const key = data.toString();
    switch (key) {
      case 'gold':
        if (GameModel.Ins().gameCoin >= 100) {
          GameModel.Ins().addGameMoney(-100);
          GameModel.Ins().cellMachine.unlockItem(UnlockItemModel.Ins().unlockCol, UnlockItemModel.Ins().unlockRow);
          this.clickClose();
        }
        break;
      case 'ad':
        // GravityModel.I.track(GEEvent.AD_SHOW, { adPosition: `看视频解锁格子` });
        MultiPlatform.Ins().platformUnit.showVideo(VideoEvent.UnlockGrid);
        break;
      case 'close':
        this.clickClose();
        break;
    }
  }
}
